package com.zr.controller.sys;

import com.zr.exception.BusinessException;
import com.zr.exception.ValidateException;
import com.zr.service.sys.SysAuthService;
import com.zr.utils.AjaxResult;
import com.zr.vo.sys.SysAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王红伟
 * 权限控制层
 */
@RestController
@RequestMapping("/sys/auth")
public class SysAuthController {

    @Autowired
    private SysAuthService sysAuthService;

    /**
     * 查询所有权限
     *
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult list() {
        return AjaxResult.success(200, "查询成功", sysAuthService.list());
    }

    /**
     * 新增权限
     *
     * @param sysAuth
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysAuth sysAuth) {
        sysAuthService.add(sysAuth);
        return AjaxResult.success(200, "新增成功", null);
    }

    /**
     * 编辑权限
     *
     * @param sysAuth
     * @return
     * @throws ValidateException 请求参数检查异常
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysAuth sysAuth) throws ValidateException {
        sysAuthService.edit(sysAuth);
        return AjaxResult.success(200, "修改成功", null);
    }


    /**
     * 删除权限
     *
     * @param id
     * @return
     * @throws BusinessException 业务逻辑异常
     */
    @RequestMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) throws BusinessException {
        sysAuthService.remove(id);
        return AjaxResult.success(200, "删除成功", null);
    }

    /**
     * 查询当前用户权限(baseResultMap)
     *
     * @return
     */
    @RequestMapping("/listByCurrentUser")
    public AjaxResult listByCurrentUser() {
        return AjaxResult.success(200, "查询成功", sysAuthService.listByCurrentUser());
    }

    /**
     * 查询当前用户权限(父带子)
     *
     * @return
     */
    @RequestMapping("/getCurentAuths")
    public AjaxResult getCurentAuths() {
        return AjaxResult.success(200, "获取用户权限列表成功", sysAuthService.getCurentAuths());
    }

    /**
     * 查询所有父权限
     *
     * @return
     */
    @RequestMapping("/getParents")
    public AjaxResult getParents() {
        return AjaxResult.success(200, "查询成功", sysAuthService.getParents());
    }
}
